/**
 * 城市默认分类系数编辑
 * @author linfeng
 * @since 2019/08/24 16:06
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;

    let laydate = layui.laydate;

    let effectiveDateObj = {
        elem: '#effectiveDate',
        type: 'date',
        btns: ['clear', 'confirm'],
        min: 1
    };

    laydate.render(effectiveDateObj);

    form.render(null, "zmn-form");
    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        if (obj.field.outRatio != null && obj.field.outRatio != undefined){
            if (!valueVerify(obj.field.outRatio)){
                return false;
            }
        }

        if (obj.field.inRatio != null && obj.field.inRatio != undefined){
            if (!valueVerify(obj.field.inRatio)){
                return false;
            }
        }

        if (obj.field.projectRatio != null && obj.field.projectRatio != undefined){
            if (!valueVerify(obj.field.projectRatio)){
                return false;
            }
        }

         if (obj.field.meterRatio != null && obj.field.meterRatio != undefined){
             if (!valueVerify(obj.field.meterRatio)){
                 return false;
             }
         }

        parent.submitCityBatchRatio(obj.field.outRatio, obj.field.inRatio, obj.field.projectRatio,obj.field.meterRatio, obj.field.effectiveDate, json.priceType);
        cancel();
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('city_batch_price_ratio_edit', {});
});

function valueVerify(value) {
    var reg=/^\d+(\.\d{1,2})?$/;

    if (value == ""){
        return true;
    }

    if (isNaN(value)){
        layer.alert("请输入正确的数字");
        return false;
    }

    // if (value == '0' || value == '0.0' || value == '0.00'){
    //     return true;
    // }

    if(!reg.test(value))
    {
        layer.alert("请输入整数或者1到2位小数");
        return false;
    }

    if ((parseFloat(0.6) <= parseFloat(value) && parseFloat(3.0) >= parseFloat(value))){
    } else{
        layer.alert("系数值不符合规定,系数值范围0.6~3.0");
        return false;
    }

    return true;
}

function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}
